package com.okaixz.web.core.config;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.check.WordChecks;
import com.github.houbb.sensitive.word.support.ignore.SensitiveWordCharIgnores;
import com.github.houbb.sensitive.word.support.resultcondition.WordResultConditions;
import com.github.houbb.sensitive.word.support.tag.WordTags;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Configuration
public class SensitiveWordConfig {
    /**
     * 初始化敏感词处理器
     */
    @PostConstruct
    public void initSensitiveWord() throws IOException {
//        // 1. 加载自定义敏感词文件
//        List<String> customWords = Files.readAllLines(
//                Paths.get(ClassLoader.getSystemResource("sensitive-words.txt").toURI())
//        );

        // 2. 初始化敏感词处理器（合并默认词库和自定义词库）
        SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()
                .ignoreCase(true)
                .ignoreWidth(true)
                .ignoreNumStyle(true)
                .ignoreChineseStyle(true)
                .ignoreEnglishStyle(true)
                .ignoreRepeat(false)
                .enableNumCheck(false)
                .enableEmailCheck(false)
                .enableUrlCheck(false)
                .enableIpv4Check(false)
                .enableWordCheck(true)
                .wordFailFast(true)
                .wordCheckNum(WordChecks.num())
                .wordCheckEmail(WordChecks.email())
                .wordCheckUrl(WordChecks.url())
                .wordCheckIpv4(WordChecks.ipv4())
                .wordCheckWord(WordChecks.word())
                .numCheckLen(8)
                .wordTag(WordTags.none())
                .charIgnore(SensitiveWordCharIgnores.defaults())
                .wordResultCondition(WordResultConditions.alwaysTrue())
                .init();



        // 3. 全局单例使用（也可注入到 Spring 容器）
        SensitiveWordHolder.setSensitiveWordBs(sensitiveWordBs);
    }

    // 单例持有类
    public static class SensitiveWordHolder {
        private static SensitiveWordBs sensitiveWordBs;

        public static SensitiveWordBs getSensitiveWordBs() {
            return sensitiveWordBs;
        }

        public static void setSensitiveWordBs(SensitiveWordBs sensitiveWordBs) {
            SensitiveWordHolder.sensitiveWordBs = sensitiveWordBs;
        }
    }
}
